<?php

namespace app\services;

use app\models\Admins;
use app\models\AdminTokens;

class AdminService
{
    /**
     * @param $account
     * @return Admins|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function check($account)
    {
        $admin = Admins::where(["account" => $account, 'status' => 1])->find();

        if ($admin) {
            return $admin;
        } else {
            throw new \Exception('管理员不存在', 500);
        }
    }

    /**
     * @param $adminId
     * @return AdminTokens|bool
     */
    public function makeToken($adminId)
    {
//        $ttl = 86400 * 7;
//        $tokens = AdminTokens::where(['adminId' => $adminId, 'status' => AdminTokens::STATUS_NORMAL])
//            ->save(['status' => AdminTokens::STATUS_VAILD]);
//
//        $token = new AdminTokens();
//        $token->adminId = $adminId;
//        $token->token = (new UtilService())->uuid();
//        $token->expiryAt = date('Y-m-d H:i:s', time() + $ttl);
//        $token->status = AdminTokens::STATUS_NORMAL;
//        if ($token->save()) {
//            return $token;
//        }
//        return false;
        $ttl = 86400 * 7;
        $token = AdminTokens::where(['adminId' => $adminId, 'status' => AdminTokens::STATUS_NORMAL])->find();

        if (!$token) {
            $token = new AdminTokens();
            $token->adminId = $adminId;
            $token->token = (new UtilService())->uuid();
            $token->expiryAt = date('Y-m-d H:i:s', time() + $ttl);
            $token->status = AdminTokens::STATUS_NORMAL;
            $token->save();
        }
        return $token;
    }

    /**
     * @param $token
     * @return Admins|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkToken($token)
    {
        $adminToken = AdminTokens::where(['token' => $token])->order(['tokenId' => 'desc'])->find();
        if (!$adminToken) {
            return null;
        }

        if ($adminToken->status == 0) {
            return null;
        }

        return $this->getAdminById($adminToken->adminId);
    }

    /**
     * @param $token
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function expiryToken($token)
    {
        $token = AdminTokens::where(['token' => $token])->find();
        if($token){
            $token->status = AdminTokens::STATUS_VAILD;
            $token->save(); 
        }
        return true;
    }

    /**
     * @param $account
     * @return Admins|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAdminByAccount($account)
    {
        $admin = Admins::where("account",$account)->find();
        return $admin;
    }

    /**
     * @param $post
     * @return Admins
     */
    public function add($post)
    {
        $admin = new Admins;
        $admin->account = $post['account'];
        $admin->status = $post['status']?$post['status']:0;
        $admin->password = md5($post['password']);
        $admin->realName = $post['realName'];
        $admin->save();
        return $admin;
    }

    /**
     * @param array $params
     * @param int $page
     * @param int $pagesize
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function list($params=[],$page=1,$pagesize=10)
    {
        return Admins::lists($params,$page,$pagesize);
    }

    /**
     * @param $id
     * @return Admins|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAdminById($id)
    {
        return Admins::find($id);
    }

    /**
     * @param Admins $admin
     * @param $post
     */
    public function update(Admins $admin, $post)
    {
        isset($post['realName']) && $admin->realName = $post['realName'];
        isset($post['password']) && $admin->password = md5($post['password']);
        isset($post['status']) && $admin->status = $post['status'];
        $admin->save();
    }

    /**
     * @param Admins $admin
     */
    public function delete(Admins $admin)
    {
        $admin->is_delete = 1;
        $admin->save();
    }
}